package alo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName test5
 * @Description TODO
 * @Author Fanxc
 * @DATE 2021/4/15 16:49
 * @Version 1.0
 */
public class test5 {
    public class Solution {
        /**
         *
         * @param root TreeNode类
         * @return int整型ArrayList<ArrayList<>>
         */
        public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
            // write code here
            Queue<TreeNode> queue=new LinkedList<>();
            ArrayList<ArrayList<Integer>> lists=new ArrayList<>();
            if (root==null) return lists;
            queue.offer(root);
            while (!queue.isEmpty()){
                ArrayList<Integer> list=new ArrayList<>();
                for (int i = queue.size(); i > 0 ; i--) {
                    TreeNode temp=queue.poll();
                    list.add(temp.val);
                    if (temp.left!=null)queue.offer(temp.left);
                    if (temp.right!=null)queue.offer(temp.right);
                }
                lists.add(list);
            }
            return lists;
        }
    }
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
